using System.IO;
using System.Xml.Serialization;
using TddGenerator.Core.Domain;
using TddGenerator.Core.Services;

namespace TddGenerator.Core.Services.Impl
{
    public class DefaultSettingsRepository:IDefaultSettingsRepository
    {
        private readonly IFileLocator fileLocator;
        private readonly ISerializer<DefaultSettings> serializer;
        private readonly IFileSystemService fileSystemService;

        public DefaultSettingsRepository(IFileLocator fileLocator, ISerializer<DefaultSettings> serializer, IFileSystemService fileSystemService)
        {
            this.fileLocator = fileLocator;
            this.serializer = serializer;
            this.fileSystemService = fileSystemService;
        }


        public DefaultSettings Get()
        {
            DefaultSettings defaultSettings;
            defaultSettings = null;
            if (fileSystemService.FileExists(fileLocator.FileName))
            {
                defaultSettings = serializer.Deserialize(fileLocator.FileName);
            }
            return defaultSettings;
        }

        public void Save(DefaultSettings defaultSettings)
        {
            serializer.Serialize(defaultSettings,fileLocator.FileName);

        }
    }
}